package com.maodou.android.viewmodel.account

import android.content.Context
import androidx.lifecycle.viewModelScope
import com.maodou.android.data.model.AccountCategoryVo
import com.maodou.android.data.remote.AccountRepository
import com.maodou.core.base.BaseViewModel
import com.maodou.core.datastore.db.AccountStore
import com.maodou.core.navigation.GlobalUiBus
import com.maodou.core.utils.Logger
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.random.Random

/**
 * @Author Xing
 * @CreateTime 2025年10月17日
 * @Description 账号分类
 */
@HiltViewModel
class CategoryViewModel @Inject constructor(
    @ApplicationContext private val appContext: Context,
    private val repository: AccountRepository,
    private val accountStore: AccountStore
) : BaseViewModel<CategoryUiState, CategoryUiEvent>() {

    override fun createInitialState(): CategoryUiState {
        return CategoryUiState()
    }

    init {
        request()
    }

    fun request(){
        viewModelScope.launch {
            updateUiState {
                it.copy(
                    isRefresh = true
                )
            }
            delay(1500)
            updateUiState {
                it.copy(
                    categoryList = mocCategoryData(),
                    isRefresh = false
                )
            }
        }
    }

    fun addCategory(name:String,block:() -> Unit) {
        if(name.isEmpty()) return
        viewModelScope.launch {
            val isExist = uiState.value.categoryList.findLast { it.name == name }
            if(isExist != null) {
                GlobalUiBus.showToast("分类已存在")
                return@launch
            }
            updateUiState {
                it.copy(
                    isLoading = true
                )
            }
            val data = ArrayList<AccountCategoryVo>()
            data.addAll(uiState.value.categoryList)
            data.add(
                AccountCategoryVo(
                    id = "${Random.nextInt()}",
                    name = name
                ),
            )
            delay(1500)
            block()
            updateUiState {
                it.copy(
                    categoryList = data,
                    isLoading = false
                )
            }
        }
    }

    fun editCategory(id:String?,name:String,block:() -> Unit) {
        if(name.isEmpty()) return
        viewModelScope.launch {
            updateUiState {
                it.copy(
                    isLoading = true
                )
            }
            val data = ArrayList<AccountCategoryVo>()
            data.addAll(uiState.value.categoryList)
            var index = -1
            data.forEachIndexed { i, accountCategoryVo ->
                if(accountCategoryVo.id == id) index = i
            }
            if(index != -1) {
                val item = AccountCategoryVo(
                    id = data[index].id,
                    name = name
                )
                data.removeAt(index)
                data.add(index,item)
            }
            delay(1500)
            block()
            updateUiState {
                it.copy(
                    categoryList = data,
                    isLoading = false
                )
            }
        }
    }

    fun deleteCategory(id:String?) {
        viewModelScope.launch {
            updateUiState {
                it.copy(
                    isLoading = true
                )
            }
            val data = ArrayList<AccountCategoryVo>()
            data.addAll(uiState.value.categoryList)
            var index = -1
            data.forEachIndexed { i, accountCategoryVo ->
                if(accountCategoryVo.id == id) index = i
            }
            data.removeAt(index)
            updateUiState {
                it.copy(
                    categoryList = data,
                    isLoading = false
                )
            }
        }
    }

}

data class CategoryUiState(
    val isLoading: Boolean = false,
    val isRefresh: Boolean = false,
    val categoryList: List<AccountCategoryVo> = arrayListOf(),
)



/**
 *  UI 事件
 */
sealed interface CategoryUiEvent


fun mocCategoryData(): List<AccountCategoryVo> {
    return listOf(
        AccountCategoryVo(
            id = "1",
            name = "企业号"
        ),
        AccountCategoryVo(
            id = "2",
            name = "BM号"
        ),
        AccountCategoryVo(
            id = "3",
            name = "黑号"
        )
    )
}